home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 474 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  82 lines

  1. Path: freya.cs.umass.edu!ramanath
  2. From: ramanath@freya.cs.umass.edu (Kumaresan Ramanathan)
  3. Newsgroups: comp.lang.c
  4. Subject: Macros in C (Disadvantages?)
  5. Date: 5 Jan 1996 17:08:33 GMT
  6. Organization: CS Dept., Umass-Amherst
  7. Message-ID: <4cjluh$gva@kernighan.cs.umass.edu>
  8. NNTP-Posting-Host: freya.cs.umass.edu
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11. Folks,
  12.   I have heard many people claim that macros are inherently dangerous and
  13. should never be used.
  14.   However, I have always found them useful for extending the language to
  15. do what C normally cannot do.
  16.  
  17.   I have been thinking about writing a general purpose code generator as
  18. freeware to provide a powerful mechanism for creating user defined
  19. extensions to C. I would not like to create something that is even worse
  20. than the current cpp! 
  21.  
  22.  I am aware of the following disadvantages to macros in C (and C++).
  23.  
  24. 1) Macros are text substitution. So 
  25.      #define square(x) x*x  is bad if I use square(x++)
  26. 2) Macros have no scope. That is {} do not nest the definition of macros
  27.  
  28. 3) Most current debuggers do not allow us to step through a block of code
  29. inside a macro. For example,
  30.   #define myfor(x,y,z) for(i=x;i<=y;i++){z}
  31.  
  32.  for(1,10,
  33.     printf("The valuse of i is");
  34.     printf(" %d \n",i);
  35.  )
  36.  
  37.  Most cpp's will reduce this to a single line. So most debuggers (on UNIX) 
  38. will not have line number info to step through the two different printf's.
  39.  
  40. Notice that I am only talking about the use of cpp to extend the C language,
  41. not about using constants as in:
  42.   #define SOMETHING 123
  43.  
  44. 4) Macros (unlike dedicated code generators) cannot create new symbols
  45. on the fly (even with contrived 'pastes', it is difficult...)
  46.  For example in the above for() example , it would have been nice to if we 
  47. could have created a new index variable for each level of nested for() so
  48. that there were no name clashes.
  49.  
  50. 5) Macros can only generate code inplace. So I cannot have a macros that
  51. is invoked using :
  52.  
  53. main()
  54. {
  55. button(OK, printf("OK was pressed");)
  56. button(CANCEL,printf("Cancel was pressed");)
  57. }
  58.  
  59. that *create* new functions like:
  60.   void gensym1()
  61.   {printf("OK was pressed"); }
  62.  
  63.  and 
  64.   void gensym2()
  65.   {printf(Cancel was pressed");}
  66.  
  67. General purpose code generators should have no trouble with this.
  68.  
  69. --------------------------------------------------------------------------
  70. So my query is specifically:
  71.   What can code generators (written from scratch) do that macros cannot.
  72.   Have I missed any disadvantages of macros (for the purpose of extending
  73. the C language) ?
  74.   Is it really necessary to extend the C language (like the way lisp users
  75.  are accustomed to... ?
  76.  
  77.   I will post a digest of responses sent to ramanath@cs.umass.edu
  78.  
  79.  
  80.   Thanks.
  81.      --- Kumaresan
  82.